import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/login/login.dart';
import 'package:sui_shou_pai/my/about_us.dart';
import 'package:sui_shou_pai/my/update_password.dart';
import 'package:sui_shou_pai/my/update_phone.dart';
import 'package:sui_shou_pai/network/http_request.dart';

import 'bottom_sheet_widget.dart';
import 'my_inform.dart';

class Setting extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('设置'),
        elevation: 0,
      ),
      body: Column(
        children: [
          _buildMyInfo(context),
          _buildUpdatePassword(context),
          _buildUpdatePhone(context),
          _buildAboutUs(context),
          _buildLogout(context),
        ],
      ),
    );
  }

  Widget _buildMyInfo(context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => MyInfo()),
        );
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: 60,
        margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
        padding: EdgeInsets.fromLTRB(20, 10, 10, 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text('我的资料', style: TextStyle(fontSize: 20)),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1,
              color: Color(0xffe5e5e5),
            ),
          ),
          color: Colors.white,
        ),
      ),
    );
  }

  Widget _buildUpdatePassword(context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => UpdatePassword()),
        );
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: 60,
        padding: EdgeInsets.fromLTRB(20, 10, 10, 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text('修改密码', style: TextStyle(fontSize: 20)),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1,
              color: Color(0xffe5e5e5),
            ),
          ),
          color: Colors.white,
        ),
      ),
    );
  }

  Widget _buildUpdatePhone(context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => UpdatePhone()),
        );
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: 60,
        padding: EdgeInsets.fromLTRB(20, 10, 10, 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text('修改手机号', style: TextStyle(fontSize: 20)),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1,
              color: Color(0xffe5e5e5),
            ),
          ),
          color: Colors.white,
        ),
      ),
    );
  }

  Widget _buildAboutUs(context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => AboutUs()),
        );
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: 60,
        padding: EdgeInsets.fromLTRB(20, 10, 10, 10),
        margin: EdgeInsets.only(bottom: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text('关于我们', style: TextStyle(fontSize: 20)),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1,
              color: Color(0xffe5e5e5),
            ),
          ),
          color: Colors.white,
        ),
      ),
    );
  }

  logout(context) async {
    HttpRequest.request("/employee/logout",
            method: "get", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        StorageUtil.clear();
        Fluttertoast.showToast(msg: "退出登录成功");
        Navigator.of(context).pushAndRemoveUntil(
            new MaterialPageRoute(builder: (context) => new Login()
            ), (route) => route == null);
      }
    });
  }

  Widget _buildLogout(context) {
    return GestureDetector(
      onTap: () {
        showDialog(
            barrierDismissible: true, //是否点击空白区域关闭对话框,默认为true，可以关闭
            context: context,
            // barrierColor: Colors.transparent,
            builder: (BuildContext context) {
              var list = [];
              list.add('退出登录');
              return BottomSheetWidget(
                list: list,
                onItemClickListener: (index) async {
                  if (index == 0) {
                    logout(context);
                  } else {
                    Navigator.pop(context);
                  }
                },
              );
            });
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: 60,
        padding: EdgeInsets.fromLTRB(20, 10, 10, 10),
        child: Center(
          child:
              Text('退出登录', style: TextStyle(fontSize: 20, color: Colors.red)),
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1,
              color: Color(0xffe5e5e5),
            ),
          ),
          color: Colors.white,
        ),
      ),
    );
  }
}
